package ui.custom.dialog;

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.HashMap;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import persistance.database.dao.EmployeeDAO;
import ui.PersonalManagerGUI;
import ui.custom.button.ConfirmButton;
import ui.custom.component.FormFields;
import ui.custom.label.AttLabel;
import ui.custom.message.InformationDialog;
import ui.custom.textfield.FormText;
import domain.valueObejcts.Employee;

public class EditPersonDialog extends JDialog {

	private static final long serialVersionUID = 1L;
	private FormText forNameTxt;
	private FormText surNameTxt;
	protected JComboBox<String> employees;
	private FormFields fields;
	private Employee employee;
	protected List<Employee> result;
	private HashMap<String, String> filter = new HashMap<String, String>();

	public EditPersonDialog(PersonalManagerGUI parent, boolean b) {
		super(parent, "Person editieren", b);
		initialize();
		pack();
        setResizable(false);
        setLocationRelativeTo(null);
        setAlwaysOnTop(true);
	}
	
	private void initialize(){
		
		final GridBagLayout gbl = new GridBagLayout();
		final GridBagConstraints gbc = new GridBagConstraints();
		final JPanel searchPanel = new JPanel(gbl);
		
		AttLabel forName = new AttLabel("Vorname:", SwingConstants.RIGHT, Font.BOLD, 12);
		gbc.gridx = 0;
		gbc.gridy = 0;
		gbl.setConstraints(forName, gbc);
		searchPanel.add(forName);
		forNameTxt = new FormText();
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbl.setConstraints(forNameTxt, gbc);
		searchPanel.add(forNameTxt);
		
		AttLabel surName = new AttLabel("Nachname:", SwingConstants.RIGHT, Font.BOLD, 12);
		gbc.gridx = 0;
		gbc.gridy = 1;
		gbl.setConstraints(surName, gbc);
		searchPanel.add(surName);
		surNameTxt = new FormText();
		gbc.gridx = 1;
		gbc.gridy = 1;
		gbl.setConstraints(surNameTxt, gbc);
		searchPanel.add(surNameTxt);
		
		JPanel btn = new JPanel(new FlowLayout());
		ConfirmButton search = new ConfirmButton("Suchen");
		search.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				filter.put("forName", forNameTxt.getText());
				filter.put("surName", surNameTxt.getText());
				result = EmployeeDAO.read(filter);
				employees.removeAllItems();
				if(result.size() >= 1){
					for (Employee employee : result) {
						employees.addItem(employee.toString());
					}
					employees.setSelectedIndex(0);
					fields.update(result.get(0));
				} else {
					fields.clear();
				}
			}
		});
		btn.add(search);

		gbc.gridx = 1;
		gbc.gridy = 2;
		gbl.setConstraints(btn, gbc);
		searchPanel.add(btn);
		
		employees = new JComboBox<String>();
		gbc.gridx = 1;
		gbc.gridy = 3;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbl.setConstraints(employees, gbc);
		searchPanel.add(employees);
		employees.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				if(employees.getItemCount() > 0) {
					employee = result.get(employees.getSelectedIndex());
					fields.update(employee);
				}
			}
		});
		
		fields = new FormFields(12);
		gbc.gridx = 0;
		gbc.gridy = 4;
		gbc.gridwidth = 2;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbl.setConstraints(fields, gbc);
		searchPanel.add(fields);
		
		fields.getOkBtn().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				EmployeeDAO.update(fields.fillEmployee(employee));
				InformationDialog.show("Person wurde erfolgreich editiert.");
			}
		});
		
		fields.getCancelBtn().addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				EditPersonDialog.this.dispose();
			}
		});
		
		add(searchPanel);
		
	}

}
